home *** CD-ROM | disk | FTP | other *** search
/ Enter 2006 September / Enter 09 2006.iso / Internet / SpamExperts Home 1.1 / SpamExperts Home.exe / lib / spamexperts.modules / dns / opcode.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-07-14  |  2.3 KB  |  89 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. '''DNS Opcodes.'''
  5. import dns.exception as dns
  6. QUERY = 0
  7. IQUERY = 1
  8. STATUS = 2
  9. NOTIFY = 4
  10. UPDATE = 5
  11. _by_text = {
  12.     'QUERY': QUERY,
  13.     'IQUERY': IQUERY,
  14.     'STATUS': STATUS,
  15.     'NOTIFY': NOTIFY,
  16.     'UPDATE': UPDATE }
  17. _by_value = []([ (y, x) for x, y in _by_text.iteritems() ])
  18.  
  19. class UnknownOpcode(dns.exception.DNSException):
  20.     '''Raised if an opcode is unknown.'''
  21.     pass
  22.  
  23.  
  24. def from_text(text):
  25.     '''Convert text into an opcode.
  26.  
  27.     @param text: the textual opcode
  28.     @type text: string
  29.     @raises UnknownOpcode: the opcode is unknown
  30.     @rtype: int
  31.     '''
  32.     if text.isdigit():
  33.         value = int(text)
  34.         if value >= 0 and value <= 15:
  35.             return value
  36.         
  37.     
  38.     value = _by_text.get(text.upper())
  39.     if value is None:
  40.         raise UnknownOpcode
  41.     
  42.     return value
  43.  
  44.  
  45. def from_flags(flags):
  46.     '''Extract an opcode from DNS message flags.
  47.  
  48.     @param flags: int
  49.     @rtype: int
  50.     '''
  51.     return (flags & 30720) >> 11
  52.  
  53.  
  54. def to_flags(value):
  55.     '''Convert an opcode to a value suitable for ORing into DNS message
  56.     flags.
  57.     @rtype: int
  58.     '''
  59.     return value << 11 & 30720
  60.  
  61.  
  62. def to_text(value):
  63.     '''Convert an opcode to text.
  64.  
  65.     @param value: the opcdoe
  66.     @type value: int
  67.     @raises UnknownOpcode: the opcode is unknown
  68.     @rtype: string
  69.     '''
  70.     text = _by_value.get(value)
  71.     if text is None:
  72.         text = str(value)
  73.     
  74.     return text
  75.  
  76.  
  77. def is_update(flags):
  78.     '''True if the opcode in flags is UPDATE.
  79.  
  80.     @param flags: DNS flags
  81.     @type flags: int
  82.     @rtype: bool
  83.     '''
  84.     if from_flags(flags) == UPDATE:
  85.         return True
  86.     
  87.     return False
  88.  
  89.